package com.sd.repay.service.impl;

import com.sd.repay.common.Constants;
import com.sd.repay.dao.BaseDao;
import com.sd.repay.pojo.RealAuthBusi;
import com.sd.repay.pojo.RealAuthChannel;
import com.sd.repay.pojo.RealAuthRecord;
import com.sd.repay.service.RealAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author sd
 * @Description: 实名认证服务类
 * @date 2017年10月25日
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class RealAuthServiceImpl implements RealAuthService {

    @Autowired
    private BaseDao baseDao;


    @Override
    public List<RealAuthChannel> getRealAuthChannel() {
        String sql = "select * from t_real_auth_channel where status = '1'";
        return baseDao.findList(RealAuthChannel.class, sql);
    }

    @Override
    public RealAuthBusi getRealAuthBusi(String realType, String realChannel, String busiStatus) {
        String sql = "select * from t_real_auth_busi where real_type = ? and real_channel = ? and busi_status = ? order by create_time desc";
        return baseDao.findFirst(RealAuthBusi.class, sql, realType, realChannel, busiStatus);
    }

    @Override
    public RealAuthBusi getRealAuthBusi(String realType, String busiStatus) {
        String sql = "select * from t_real_auth_busi where real_type = ? and busi_status = ? order by create_time desc";
        return baseDao.findFirst(RealAuthBusi.class, sql, realType, busiStatus);
    }

    @Override
    public RealAuthRecord getRealAuthRecord(RealAuthRecord realAuthRecord) {
        List<String> condition = new ArrayList<>();
        String sql = "select * from t_real_auth_record where auth_type = ? ";
        String authType = realAuthRecord.getAuthType();

        condition.add(authType);
        if (authType.equals(Constants.AUTH_TYPE_TWO)) {
            sql += " and rean_name = ? and id_card_no = ?";
            condition.add(realAuthRecord.getReanName());
            condition.add(realAuthRecord.getIdCardNo());
        } else if (authType.equals(Constants.AUTH_TYPE_THREE)) {
            sql += " and rean_name = ? and id_card_no = ? and account_no = ?";
            condition.add(realAuthRecord.getReanName());
            condition.add(realAuthRecord.getIdCardNo());
            condition.add(realAuthRecord.getAccountNo());
        } else if (authType.equals(Constants.AUTH_TYPE_FOUR)) {
            sql += " and rean_name = ? and id_card_no = ? and account_no = ? and mobile_no = ?";
            condition.add(realAuthRecord.getReanName());
            condition.add(realAuthRecord.getIdCardNo());
            condition.add(realAuthRecord.getAccountNo());
            condition.add(realAuthRecord.getMobileNo());
        }

        sql += " and auth_status in('1','2') order by create_time desc";
        return baseDao.findFirst(RealAuthRecord.class, sql, condition.toArray());
    }

    @Override
    public RealAuthRecord getRealAuthRecord(String realId) {
        String sql = "select * from t_real_auth_record where id = ? and auth_status='0'";
        return baseDao.findFirst(RealAuthRecord.class, sql, realId);
    }

    /**
     * 新增实名认证记录
     *
     * @param recordMap
     * @return
     * @throws Exception
     */
    @Override
    public Long saveRealAuthRecord(Map<String, String> recordMap) throws Exception {

        String sql = " INSERT INTO t_real_auth_record(record_no,auth_channel,auth_source,rean_name,id_card_no,account_no,mobile_no,auth_type,create_time) " +
                " VALUES(?,?,?,?,?,?,?,?,?)";
        List<Object> params = new ArrayList<>();
        params.add(recordMap.get("recordNo"));
        params.add(recordMap.get("authChannel"));
        params.add(recordMap.get("authSource"));
        params.add(recordMap.get("realName"));
        params.add(recordMap.get("idCardNo"));
        params.add(recordMap.get("accountNo"));
        params.add(recordMap.get("mobileNo"));
        params.add(recordMap.get("authType"));
        params.add(new Date());
        return baseDao.getSaveObjId(sql, params.toArray());
    }

    /**
     * 修改实名认证记录
     *
     * @param realAuthRecord 实名认证记录
     */
    @Override
    public int updateRealAuthStatus(RealAuthRecord realAuthRecord) throws Exception {
        List<String> condition = new ArrayList<>();
        condition.add(realAuthRecord.getAuthStatus());
        condition.add(realAuthRecord.getRespCode());
        condition.add(realAuthRecord.getRespMsg());
        condition.add(realAuthRecord.getBak1());
        condition.add(realAuthRecord.getBak2());
        condition.add(String.valueOf(realAuthRecord.getId()));

        String sql = " UPDATE t_real_auth_record SET auth_status = ?, resp_code = ?, resp_msg = ?, bak1 =?, bak2 = ? WHERE id = ? and auth_status = '0'";
        return baseDao.execute(sql, condition.toArray());
    }
}